{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "837095db",
   "metadata": {
    "code_folding": [
     0
    ]
   },
   "outputs": [],
   "source": [
    "#全部行都能输出\n",
    "from IPython.core.interactiveshell import InteractiveShell\n",
    "InteractiveShell.ast_node_interactivity = \"all\"\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# 解决坐标轴刻度负号乱码\n",
    "plt.rcParams['axes.unicode_minus'] = False\n",
    "# 解决中文乱码问题\n",
    "plt.rcParams['font.sans-serif'] = ['Simhei']\n",
    "plt.style.use('ggplot')\n",
    "# plt.figure(figsize=(2,3),dpi=720)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2f7981c1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>颜色深度</th>\n",
       "      <th>酒精浓度</th>\n",
       "      <th>品种</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>14.13</td>\n",
       "      <td>5.64</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>13.20</td>\n",
       "      <td>4.28</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>13.16</td>\n",
       "      <td>5.68</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>14.27</td>\n",
       "      <td>4.80</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>13.24</td>\n",
       "      <td>4.22</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>12.07</td>\n",
       "      <td>2.76</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>12.43</td>\n",
       "      <td>3.94</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>11.79</td>\n",
       "      <td>3.10</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>12.37</td>\n",
       "      <td>2.12</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>12.04</td>\n",
       "      <td>2.60</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    颜色深度  酒精浓度  品种\n",
       "0  14.13  5.64   0\n",
       "1  13.20  4.28   0\n",
       "2  13.16  5.68   0\n",
       "3  14.27  4.80   0\n",
       "4  13.24  4.22   0\n",
       "5  12.07  2.76   1\n",
       "6  12.43  3.94   1\n",
       "7  11.79  3.10   1\n",
       "8  12.37  2.12   1\n",
       "9  12.04  2.60   1"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rowdata = {'颜色深度':\n",
    "[14.13,13.2,13.16,14.27,13.24,12.07,12.43,11.79,12.37,12.04],\n",
    "'酒精浓度': [5.64,4.28,5.68,4.80,4.22,2.76,3.94,3.1,2.12,2.6],\n",
    "'品种': [0,0,0,0,0,1,1,1,1,1]}\n",
    "# 0 代表 “黑皮诺”，1 代表 “赤霞珠”\n",
    "wine_data = pd.DataFrame(rowdata)\n",
    "wine_data\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "7c6515aa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[14.13,  5.64],\n",
       "       [13.2 ,  4.28],\n",
       "       [13.16,  5.68],\n",
       "       [14.27,  4.8 ],\n",
       "       [13.24,  4.22],\n",
       "       [12.07,  2.76],\n",
       "       [12.43,  3.94],\n",
       "       [11.79,  3.1 ],\n",
       "       [12.37,  2.12],\n",
       "       [12.04,  2.6 ]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = np.array(wine_data.iloc[0:,0:2]) #我们把特征（酒的属性）放在X\n",
    "y = np.array(wine_data.iloc[:,-1]) #把标签（酒的类别）放在Y\n",
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "25df55b3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.collections.PathCollection at 0x115224f7f70>"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "<matplotlib.collections.PathCollection at 0x11523d34550>"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "<matplotlib.collections.PathCollection at 0x1151e6d5cc0>"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "array([12.03,  4.1 ])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "Text(0.5, 0, '酒精浓度')"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "Text(0, 0.5, '颜色深度')"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "<matplotlib.legend.Legend at 0x1152393c430>"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYMAAAEGCAYAAACHGfl5AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/YYfK9AAAACXBIWXMAAAsTAAALEwEAmpwYAAAsdElEQVR4nO3de1zUdb7H8RfMcBUIBEWJoNCoTA2TOqtu5TG6WJZpbFlra2vuaoGbbieqLQ1PmoSdo4mbVlpZu8l2MaptzWTN1sxUJPOKd0AFURMEHbnM5fzBNidkQFQGZpj385/gN9/5+fnM79F8+H5/v+/362Wz2WyIiIhH827vAEREpP2pGIiIiIqBiIioGIiICCoGIiKCioGIiKBiICIigLG9AzhfJSUl7R1CsyIiIjh27Fh7h9GmPDFn8My8lbN7ioqKavI19QxERETFQEREVAxERAQVAxERQcVARERw46eJRDqSyuJK8jLzMB02EdgtkMS0REJiQto7LPEgKgYi7ayyuJJlo5ZRWVRpP3Y0/yhDs4eqIEib0TCRSDvLy8xrUAgAKovqewri/iqLK1mZupK/J/+dlakrqSyuPPub2oF6BiLtzHTY5Ph4mePj4j7cqdennoFIOwvsFuj4eKTj4+I+3KnXp2Ig0s4S0xIJiW34V2JIbAiJaYntFJG0Fnfq9WmYSKSdhcSEMDR7aP3TRGUmAiP1NFFH4U69PhUDERcQEhPCkHlD2jsMaWWJaYkczT/aYKjIVXt9KgYiIk7iTr0+FQMRESdyl16fU4uBxWIhNTWVyMhIAMaOHUtMTEyjdmlpaQQG1o+hjRw5kr59+zozLBEROYNTi0FRURGDBg1i9OjRTbapqqoiKiqKSZMmOTMUERFphlOLwe7du9mwYQM7d+6kS5cupKSkYDAYGrXZuXMnU6dOJSAggMcff9zeSxARkbbhZbPZbM46+Z49ewgPDycsLIyFCxeSkJBAYmLDu+iHDh3C29ub7t278+mnn2I0GrnjjjsanSs3N5fc3FwAMjIyqK2tdVbYrcJoNGI2m9s7jDbliTmDZ+atnN2Tr69vk685tWcQGxuLj48PABdffDGlpaWN2kRGRtp7C1FRUWzevNnhuZKSkkhKSrL/7up7kXaE/VLPlSfmDJ6Zt3J2T+22B3JWVhaFhYVYrVbWr19PbGxsozZLlixh48aNAHz33XcO24iIiHM5tRgkJyczb948nnzySeLj44mLi2PBggUN2gwbNoylS5fyxBNPYDQauemmm5wZkoiIOODUewbOVFJS0t4hNKsjdCnPlSfmDJ6Zt3J2T+02TCQiIu5BxUBERFQMRERExUBERFAxEBERVAxERAQVAxERQcVARERQMRAREVQMREQEFQMREUHFQEREUDEQERFUDEREBBUDERFBxUBERFAxEBERVAxERAQVAxERQcVAREQAozNPbrFYSE1NJTIyEoCxY8cSExPTqN3777/P999/T8+ePXnkkUecGZKIiDjg1GJQVFTEoEGDGD16dJNt9u7dS0FBAS+++CKffPIJmzdvpm/fvs4MS0REzuDUYrB79242bNjAzp076dKlCykpKRgMhgZtduzYwX/8x3/g5eVFnz592Lhxo8NikJubS25uLgAZGRlEREQ4M/QLZjQaXT7G1uaJOYNn5q2cOx6nFoMePXqQnp5OWFgYCxcu5PvvvycxMbFBm+rqavswUkBAABUVFQ7PlZSURFJSkv33Y8eOOS3u1hAREeHyMbY2T8wZPDNv5eyeoqKimnzNqcUgNjYWHx8fAC6++GJKS0sbtfH396e2thaoLww2m82ZIYmIiANOfZooKyuLwsJCrFYr69evJzY2tlGbuLg4CgoKgPp7DF26dHFmSCIi4oBTi0FycjLz5s3jySefJD4+nri4OBYsWNCgzZVXXklhYSFvvfUWOTk5/PKXv3RmSCIi4oCXzQXGZWpra8nPz+eyyy6z3z84m5KSEidHdWE6wvjiufLEnMEz81bO7qnd7hm0lK+vL7/4xS/aOwwREY+lGcgiIqJiICIiLjJMJCIizassriQvMw/TYROB3QJJTEskJCak1c6vYiAi4uIqiytZNmoZlUWV9mNH848yNHtoqxUEDROJiLi4v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2KxsHnzZqqqqrjxxhvt/56vry8zZswgNzeXXbt2MWTIEPu5pk2b1lYfjXgwDROJxzp69Kj9548++si+KUv//v2ZMmWK/WbvP//5T8LCwvjxxx959dVXKSwspLS0lB49etCjRw+qqqrYunWr/VxGo5GIiAgAtmzZwt///neuv/564uPjefvtt3n33Xcb3F+oqakhJCQEs9nMpZdeSlRUFAUFBRQUFDB8+PC2+ChE1DMQz1RSUkJmZiavvPIKtbW1xMfHU1tby6pVq4iLiyMuLo6cnBwOHz7M5s2b6devH/369aNTp07U1NSwZcsW+xLNFRUVDB482H7uw4cPM2vWLB577DFef/114uPjAbjzzjv505/+xJw5cwgPD7e3P378OPHx8fzlL39h48aNAPbXd+/eTUhICL/85S/b6JMRT6ViIB7pvffe46677iI0NBSgwZfzT2w2G/v27SM5Odm+hPOIESP47rvvSEpK4p577gFg1apVVFRU2N+3Zs0abrjhBlavXs3jjz/OihUrgPrhqUGDBvGPf/yDhx56yN6+qKiIG2+8keuvv57hw4eTmZnJ5MmT+eGHHwgNDVUhkDahYiAe59tvv2XHjh2kpqYC9TdpHa1Xb7Va7Vsa/lQMfjqem5vLpk2bgIY9g7q6OnJzc5kyZYp9Cecvv/ySJUuWEBwcTEpKClOnTuWNN95g9OjR2Gw2Dh48aN9CNCwsjF/96lc8/fTTBAUFafKZtBkVA/E4CQkJTJgwAX9/fwCCgoKYNWtWo3aPPfaY/Wez2Wx/vNNsNtt7BiUlJbz22mv2jVx2795NdHR0g7X8i4uLiYuLY9y4cRiNRqZOncqiRYuoqKhg3759JCYmUlBQQH5+PgcOHMBsNnPnnXdSXFzMM888Q0hICBdddBG/+c1vmtxeVORCadKZSAvU1dVhMBgazA+A+qEkq9WKwWBo8r1Hjhyha9euTb5uNpupqqqiqKiIuLg4+1NNPykrK6O0tJSEhIQLykGkOSoGIiKiR0tFRETFQEREUDEQERFUDEREBBUDEREB/g8ldx3o+GL1mAAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#探索数据，假如我们给出新数据[12.03,4.1] ，你能猜出这杯红酒是什么类别么?\n",
    "new_data = np.array([12.03,4.1])\n",
    "plt.scatter(X[y==1,0], X[y==1,1], color='red', label='赤霞珠') #画出标签y为1的、关于“赤霞珠”的散点\n",
    "plt.scatter(X[y==0,0], X[y==0,1], color='purple', label='黑皮诺') #画出标签y为0的、关于“黑皮诺”的散点\n",
    "plt.scatter(new_data[0],new_data[1], color='yellow') # 新数据点\n",
    "new_data\n",
    "plt.xlabel('酒精浓度')\n",
    "plt.ylabel('颜色深度')\n",
    "plt.legend(loc='lower right')\n",
    "plt.savefig('葡萄酒样本.png')\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "10bd4556",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.neighbors import KNeighborsClassifier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "f7b1319d",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\sklearn\\base.py:450: UserWarning: X does not have valid feature names, but KNeighborsClassifier was fitted with feature names\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([1], dtype=int64)"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 0 代表 “黑皮诺”，1 代表 “赤霞珠”\n",
    "clf = KNeighborsClassifier(n_neighbors = 3)\n",
    "clf = clf.fit(wine_data.iloc[:,0:2], wine_data.iloc[:,-1])\n",
    "clf.predict([[12.01,4.1]]) # 返回预测的标签\n",
    "# result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "0eec72fa",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\sklearn\\base.py:450: UserWarning: X does not have valid feature names, but KNeighborsClassifier was fitted with feature names\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "1.0"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 对模型进行一个评估，接口score返回预测的准确率\n",
    "score = clf.score([[12.8,4.1]],[0])\n",
    "score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "7f513a83",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\sklearn\\base.py:450: UserWarning: X does not have valid feature names, but KNeighborsClassifier was fitted with feature names\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[0.66666667, 0.33333333]])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf.predict_proba([[12.8,4.1]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "9dabde7f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>平均半径</th>\n",
       "      <th>平均纹理</th>\n",
       "      <th>平均周长</th>\n",
       "      <th>平均面积</th>\n",
       "      <th>平均光滑度</th>\n",
       "      <th>平均紧凑度</th>\n",
       "      <th>平均凹度</th>\n",
       "      <th>平均凹点</th>\n",
       "      <th>平均对称</th>\n",
       "      <th>平均分形维数</th>\n",
       "      <th>...</th>\n",
       "      <th>最差纹理</th>\n",
       "      <th>最差的边界</th>\n",
       "      <th>最差的区域</th>\n",
       "      <th>最差的平滑度</th>\n",
       "      <th>最差的紧凑性</th>\n",
       "      <th>最差的凹陷</th>\n",
       "      <th>最差的凹点</th>\n",
       "      <th>最差的对称性</th>\n",
       "      <th>最差的分形维数</th>\n",
       "      <th>患病否</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>17.99</td>\n",
       "      <td>10.38</td>\n",
       "      <td>122.80</td>\n",
       "      <td>1001.0</td>\n",
       "      <td>0.11840</td>\n",
       "      <td>0.27760</td>\n",
       "      <td>0.3001</td>\n",
       "      <td>0.14710</td>\n",
       "      <td>0.2419</td>\n",
       "      <td>0.07871</td>\n",
       "      <td>...</td>\n",
       "      <td>17.33</td>\n",
       "      <td>184.60</td>\n",
       "      <td>2019.0</td>\n",
       "      <td>0.1622</td>\n",
       "      <td>0.6656</td>\n",
       "      <td>0.7119</td>\n",
       "      <td>0.2654</td>\n",
       "      <td>0.4601</td>\n",
       "      <td>0.11890</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>20.57</td>\n",
       "      <td>17.77</td>\n",
       "      <td>132.90</td>\n",
       "      <td>1326.0</td>\n",
       "      <td>0.08474</td>\n",
       "      <td>0.07864</td>\n",
       "      <td>0.0869</td>\n",
       "      <td>0.07017</td>\n",
       "      <td>0.1812</td>\n",
       "      <td>0.05667</td>\n",
       "      <td>...</td>\n",
       "      <td>23.41</td>\n",
       "      <td>158.80</td>\n",
       "      <td>1956.0</td>\n",
       "      <td>0.1238</td>\n",
       "      <td>0.1866</td>\n",
       "      <td>0.2416</td>\n",
       "      <td>0.1860</td>\n",
       "      <td>0.2750</td>\n",
       "      <td>0.08902</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>19.69</td>\n",
       "      <td>21.25</td>\n",
       "      <td>130.00</td>\n",
       "      <td>1203.0</td>\n",
       "      <td>0.10960</td>\n",
       "      <td>0.15990</td>\n",
       "      <td>0.1974</td>\n",
       "      <td>0.12790</td>\n",
       "      <td>0.2069</td>\n",
       "      <td>0.05999</td>\n",
       "      <td>...</td>\n",
       "      <td>25.53</td>\n",
       "      <td>152.50</td>\n",
       "      <td>1709.0</td>\n",
       "      <td>0.1444</td>\n",
       "      <td>0.4245</td>\n",
       "      <td>0.4504</td>\n",
       "      <td>0.2430</td>\n",
       "      <td>0.3613</td>\n",
       "      <td>0.08758</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>11.42</td>\n",
       "      <td>20.38</td>\n",
       "      <td>77.58</td>\n",
       "      <td>386.1</td>\n",
       "      <td>0.14250</td>\n",
       "      <td>0.28390</td>\n",
       "      <td>0.2414</td>\n",
       "      <td>0.10520</td>\n",
       "      <td>0.2597</td>\n",
       "      <td>0.09744</td>\n",
       "      <td>...</td>\n",
       "      <td>26.50</td>\n",
       "      <td>98.87</td>\n",
       "      <td>567.7</td>\n",
       "      <td>0.2098</td>\n",
       "      <td>0.8663</td>\n",
       "      <td>0.6869</td>\n",
       "      <td>0.2575</td>\n",
       "      <td>0.6638</td>\n",
       "      <td>0.17300</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>20.29</td>\n",
       "      <td>14.34</td>\n",
       "      <td>135.10</td>\n",
       "      <td>1297.0</td>\n",
       "      <td>0.10030</td>\n",
       "      <td>0.13280</td>\n",
       "      <td>0.1980</td>\n",
       "      <td>0.10430</td>\n",
       "      <td>0.1809</td>\n",
       "      <td>0.05883</td>\n",
       "      <td>...</td>\n",
       "      <td>16.67</td>\n",
       "      <td>152.20</td>\n",
       "      <td>1575.0</td>\n",
       "      <td>0.1374</td>\n",
       "      <td>0.2050</td>\n",
       "      <td>0.4000</td>\n",
       "      <td>0.1625</td>\n",
       "      <td>0.2364</td>\n",
       "      <td>0.07678</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 31 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "    平均半径   平均纹理    平均周长    平均面积    平均光滑度    平均紧凑度    平均凹度     平均凹点    平均对称  \\\n",
       "0  17.99  10.38  122.80  1001.0  0.11840  0.27760  0.3001  0.14710  0.2419   \n",
       "1  20.57  17.77  132.90  1326.0  0.08474  0.07864  0.0869  0.07017  0.1812   \n",
       "2  19.69  21.25  130.00  1203.0  0.10960  0.15990  0.1974  0.12790  0.2069   \n",
       "3  11.42  20.38   77.58   386.1  0.14250  0.28390  0.2414  0.10520  0.2597   \n",
       "4  20.29  14.34  135.10  1297.0  0.10030  0.13280  0.1980  0.10430  0.1809   \n",
       "\n",
       "    平均分形维数  ...   最差纹理   最差的边界   最差的区域  最差的平滑度  最差的紧凑性   最差的凹陷   最差的凹点  \\\n",
       "0  0.07871  ...  17.33  184.60  2019.0  0.1622  0.6656  0.7119  0.2654   \n",
       "1  0.05667  ...  23.41  158.80  1956.0  0.1238  0.1866  0.2416  0.1860   \n",
       "2  0.05999  ...  25.53  152.50  1709.0  0.1444  0.4245  0.4504  0.2430   \n",
       "3  0.09744  ...  26.50   98.87   567.7  0.2098  0.8663  0.6869  0.2575   \n",
       "4  0.05883  ...  16.67  152.20  1575.0  0.1374  0.2050  0.4000  0.1625   \n",
       "\n",
       "   最差的对称性  最差的分形维数  患病否  \n",
       "0  0.4601  0.11890  0.0  \n",
       "1  0.2750  0.08902  0.0  \n",
       "2  0.3613  0.08758  0.0  \n",
       "3  0.6638  0.17300  0.0  \n",
       "4  0.2364  0.07678  0.0  \n",
       "\n",
       "[5 rows x 31 columns]"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "from sklearn.datasets import load_breast_cancer\n",
    "from sklearn.model_selection import train_test_split\n",
    "#读取数据集\n",
    "data = load_breast_cancer()\n",
    "#DateFrame格式显示\n",
    "X = data.data\n",
    "y = data.target\n",
    "name = ['平均半径','平均纹理','平均周长','平均面积',\n",
    "'平均光滑度','平均紧凑度','平均凹度',\n",
    "'平均凹点','平均对称','平均分形维数',\n",
    "'半径误差','纹理误差','周长误差','面积误差',\n",
    "'平滑度误差','紧凑度误差','凹度误差',\n",
    "'凹点误差','对称误差',\n",
    "'分形维数误差','最差半径','最差纹理',\n",
    "'最差的边界','最差的区域','最差的平滑度',\n",
    "'最差的紧凑性','最差的凹陷','最差的凹点',\n",
    "'最差的对称性','最差的分形维数','患病否']\n",
    "data=np.concatenate((X,y.reshape(-1,1)),axis=1)\n",
    "table=pd.DataFrame(data=data,columns=name)\n",
    "table.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "4423aa31",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9210526315789473"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 划分训练集和测试集 #30%数据作为训练集\n",
    "Xtrain,Xtest,Ytrain,Ytest = train_test_split(X,y,test_size=0.2,random_state=420)\n",
    "# 建立模型&评估模型\n",
    "clf = KNeighborsClassifier(n_neighbors=4)\n",
    "# 建立分类器\n",
    "clf = clf.fit(Xtrain,Ytrain)\n",
    "score = clf.score(Xtest,Ytest)\n",
    "score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "fa7cb34d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[35.70015941, 42.02374599, 81.82147557, 83.06271326],\n",
       "        [11.81126721, 14.5871725 , 17.4734004 , 18.94892695]]),\n",
       " array([[112, 221, 303, 263],\n",
       "        [268, 162,  42, 134]], dtype=int64))"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf.kneighbors(Xtest[[20,30],:],return_distance=True)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "10ea5396",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([<matplotlib.axis.XTick at 0x115333ac7c0>,\n",
       "  <matplotlib.axis.XTick at 0x115333ac580>,\n",
       "  <matplotlib.axis.XTick at 0x115333ac0a0>,\n",
       "  <matplotlib.axis.XTick at 0x115336c3d60>,\n",
       "  <matplotlib.axis.XTick at 0x11533482920>,\n",
       "  <matplotlib.axis.XTick at 0x11523d6d900>,\n",
       "  <matplotlib.axis.XTick at 0x115336c28c0>,\n",
       "  <matplotlib.axis.XTick at 0x11523d6ca90>,\n",
       "  <matplotlib.axis.XTick at 0x11523d6ceb0>,\n",
       "  <matplotlib.axis.XTick at 0x11522774ca0>,\n",
       "  <matplotlib.axis.XTick at 0x11533d864a0>,\n",
       "  <matplotlib.axis.XTick at 0x11533d843a0>,\n",
       "  <matplotlib.axis.XTick at 0x11522776dd0>,\n",
       "  <matplotlib.axis.XTick at 0x115336c03a0>,\n",
       "  <matplotlib.axis.XTick at 0x11533d86860>,\n",
       "  <matplotlib.axis.XTick at 0x11533d85c30>,\n",
       "  <matplotlib.axis.XTick at 0x11533d87ca0>,\n",
       "  <matplotlib.axis.XTick at 0x115227b5e40>,\n",
       "  <matplotlib.axis.XTick at 0x11533ecee30>],\n",
       " [Text(0, 0, ''),\n",
       "  Text(0, 0, ''),\n",
       "  Text(0, 0, ''),\n",
       "  Text(0, 0, ''),\n",
       "  Text(0, 0, ''),\n",
       "  Text(0, 0, ''),\n",
       "  Text(0, 0, ''),\n",
       "  Text(0, 0, ''),\n",
       "  Text(0, 0, ''),\n",
       "  Text(0, 0, ''),\n",
       "  Text(0, 0, ''),\n",
       "  Text(0, 0, ''),\n",
       "  Text(0, 0, ''),\n",
       "  Text(0, 0, ''),\n",
       "  Text(0, 0, ''),\n",
       "  Text(0, 0, ''),\n",
       "  Text(0, 0, ''),\n",
       "  Text(0, 0, ''),\n",
       "  Text(0, 0, '')])"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x11533d873d0>]"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXgAAAD2CAYAAADcUJy6AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/YYfK9AAAACXBIWXMAAAsTAAALEwEAmpwYAAAlx0lEQVR4nO3de3DU9b3/8ec3BHIhkBgCQggSAoSqCESlcjUKEbzEw6+GoRU7tn94Ojrtb3qcnnbOjCIBaaWX0dYei55WWyzO4XfwKIOooDHcFES8BDQQgySES5YsgUTInWU/vz82iUISkr1ld7/7esx0mrDf73vfu25e+ea77/18LWOMQUREbCcm1A2IiEhwKOBFRGxKAS8iYlMKeBERm1LAi4jYlAJeRMSmYkPdwLdVV1f7tF9aWhq1tbV+3bdqqEa41wiHHlQj/Gqkp6f3eJuO4EVEbEoBLyJiUwp4ERGbUsCLiNiUAl5ExKYU8CIiNqWAFxGxKQW8hC3zyQe4Tp0MdRthwbQ00fTORszFi6FuRSKIAl7Ckjl1Avfzv+X8354OdSthwWx9nfNrfof55INQtyIRRAEvYckUvwlA2yd7MDW+fcLZLsyFC5gdWzxfF28OcTcSSRTwEnZMcxNmdzFcnwMDBmC2vRnqlkLKfPw+nP+aQdPnwJEyTNVXoW5JIoQCXsKO2f0etDYTs+iHxM+ej/mgCNPSFOq2QsIYg3nvDRg1huSfL4O4eMx7OoqXvlHAS1gxbrfn9EzWJKxxE0m4ezG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ixY6xatcqnx3L27FkefvjhztdJX39muuvjt7/9LZWVlT718T//8z+dPfzbv/0br7/+utc1ampqWLlyJY899hivvvqqT31UVFTw5JNP8vjjj/PGG2/0uUZvIv6KTg0NDTz33HO0trb6tP+uXbvIz89nypQp/PWvf6WkpISbb77ZqxrHjx/nRz/6EdnZ2TQ0NFBRUcG0adN86uef//wnbW1tPu1bVVXF7Nmz+eEPf+jT/h0OHTpEfX29189DhwULFrBgwQIAXnrpJW677Tava+zcuZO5c+cyZ84cnn32WY4cOcL48eP7vP+6desoKCggOzubZ555htLSUq6//vo+73/562rLli1kZWWxZMkS/vCHPzBz5kwSEhL6vP+pU6dYt24dLS0tPvfw7rvv8tBDD5GRkcFvfvMbjh07xtixY72qceLECR555BFGjhzJsWPHcDqdpKdf+YL13f2MGWN4+eWX+/wL4vIahw8f5r777ut8nfhSAzw/v1dffTXjxo3zqcaSJUs6b3v66afJzc31usaWLVv4/ve/z6RJk1i2bBkLFixg6NChXtX4+9//zs9//nOGDRvGsmXLuOWWWxgxYkSfHtOVRPwRfExMDI8++ugVf9iuZOHChUyZMgWAc+fO9fofpjtTpkwhOzubgwcPcuTIEbKzs33q5YsvviAuLo6UlBSf9j98+DD79u1j2bJlPPvss1y8eNHrGi6XixdeeIHhw4ezb98+n/rocPbsWerr68nKyvJ63yFDhlBdXU1jYyNnzpwhLS3Nq/0dDkfn/SYnJ9PU1OTV/pe/rkpLS5k1axYA2dnZHDlyxKv9ExIS+MUvfuFXD/fffz8ZGRkAnD9/niFDhnhdY/bs2QwfPpxPP/2UxsZGRo4c6XUNgG3btnn1C/PyGocPH2br1q089thj/OMf//CpRkNDAy+//DKDBw/miy++8KlGh6+++orU1FRSU1O9rjFkyBBOnjxJfX09LpeLxMREnx5LWloalmWRlJTk9eu1x/sJSJUQSkxM7NMT2pvy8nIaGxt9DmdjDLt372bAgAHExHj/tLpcLl599VUeeOABn+4fYPz48RQWFvLkk0+SmJjIZ5995nWNnTt3kpGRwaJFi/jqq694++23f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8Pm/coaSkhGuvvdanfQG+973v8Ze//IXTp0+TnZ3NnDlzvK6xadMm8vPziYuL87mPDrm5uTz11FMcOnSIkydP+vXLy1cbN26ktraWl156CfC8QXjdddd5VWPRokX8+c9/xrIspkyZ0usbrN3505/+1Pl1YWEhP/jBD7yusXjxYlasWEFsbCx33HGHT338y7/8Cy+88AKvv/46gwYN4t///d+9rgGwf/9+r043XW7JkiWdk0A33nijz3+1rl+/ngceeCBgR++gT7KK9NnZs2cpKytj2rRpAXnfRyTYFPAiIjYV9efgRUTsSgEvImJTCngREZtSwIuI2JQCXkTEpv4/j9MqPskQvqUAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "score = [ ]\n",
    "krange = range(1,20)\n",
    "\n",
    "for i in krange:\n",
    "    clf = KNeighborsClassifier(n_neighbors=i)\n",
    "    clf = clf.fit(Xtrain,Ytrain)\n",
    "    score.append(clf.score(Xtest,Ytest))\n",
    "plt.xticks(range(1,20))\n",
    "plt.plot(krange,score)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "465efa03",
   "metadata": {},
   "source": [
    "### 8.1作业"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "25425726",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from sklearn.datasets import load_iris\n",
    "from sklearn.model_selection import train_test_split, GridSearchCV, cross_val_score\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.metrics import accuracy_score,precision_score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "0ccef0e5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'C': 0.41944444444444445, 'solver': 'sag'} 0.9714285714285715\n"
     ]
    }
   ],
   "source": [
    "pre_data = load_iris()\n",
    "X = pre_data.data\n",
    "y = pre_data.target\n",
    "data = pd.DataFrame(X,columns=pre_data.feature_names)\n",
    "Xtrain, Xtest, Ytrain, ytest =train_test_split(X,y,test_size=0.3,random_state=420)\n",
    "scaler = StandardScaler()\n",
    "std = scaler.fit(Xtrain)\n",
    "Xtrain_ = std.transform(Xtrain)\n",
    "Xtest_ = std.transform(Xtest)\n",
    "p = {\n",
    "        \"C\":list(np.linspace(0.05,1,19)),\n",
    "        \"solver\":['liblinear','sag','newton-cg',\"lbfgs\"]\n",
    "    }\n",
    "model = LogisticRegression(penalty='l2',max_iter=10000)\n",
    "GS=GridSearchCV(model,p,cv=5)\n",
    "GS.fit(Xtrain_,Ytrain)\n",
    "print(GS.best_params_,GS.best_score_)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "0e50ce45",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.9714285714285714 0.9555555555555556\n",
      "0.9714285714285714 0.9555555555555556\n"
     ]
    }
   ],
   "source": [
    "bp = GS.best_params_\n",
    "model = LogisticRegression(penalty='l2',max_iter=10000,C=bp[\"C\"],solver=bp[\"solver\"])\n",
    "model.fit(Xtrain_,Ytrain)\n",
    "print(model.score(Xtrain_,Ytrain),model.score(Xtest_,ytest))\n",
    "print(precision_score(model.predict(Xtrain_),Ytrain,average='micro'),precision_score(model.predict(Xtest_),ytest,average='micro'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "742a78dc",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.1"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "384px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
